<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body{
            font-family: Arial, sans-serif;
            background-color: #663399;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: white;
        }
        fieldset{
            background-color: white;
            border: 2px solid #ddd;
            border-radius: 8px;
            padding: 30px 20px;
            width: 350px;
            color: #333;
            min-height: 300px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        legend{
            font-size: 1.3em;
            font-weight: bold;
            padding: 0;
            color: #663399;
            text-align: center;
            width: 100%;
            margin-bottom: 20px;
            border: none;
        }
        .form-group{
            margin-bottom: 25px;
            text-align: center;
        }
        label{
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
            text-align: center;
        }
        input[type="text"],
        input[type="password"]{
            width: 80%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            margin: 0 auto;
            display: block;
            text-align: center;
        }
        .button-container {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }
        .button-group {
            display: inline-flex;
            gap: 20px;
        }
        button{
            padding: 12px 30px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            border: none;
            min-width: 100px;
        }
        button[type="submit"] {
            background-color: #663399;
            color: white;
        }
        button[type="submit"]:hover {
            background-color: #5a2d8a;
        }
        button[type="reset"] {
            background-color: #f0f0f0;
            color: #333;
            border: 1px solid #ddd;
        }
        button[type="reset"]:hover {
            background-color: #e0e0e0;
        }
        .error{
            color: red;
            font-size: 0.8em;
            margin-top: 5px;
            display: none;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="loginForm">
        <fieldset>
            <legend>用户登录</legend>
            <div class="form-group">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username">
                <div id="usernameError" class="error">请输入8-20个字符的用户名</div>
            </div>
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password">
                <div id="passwordError" class="error">请输入8-20个字符的密码</div>
            </div>
            <div class="button-group">
                <button type="submit">提交</button>
                <button type="reset">重置</button>
            </div>
        </fieldset>
    </form>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            // 获取输入值
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();

            // 重置错误提示
            document.getElementById('usernameError').style.display = 'none';
            document.getElementById('passwordError').style.display = 'none';

            // 验证用户名
            if (!username) {
                document.getElementById('usernameError').textContent = '用户名不能为空';
                document.getElementById('usernameError').style.display = 'block';
                document.getElementById('username').focus();
                return;
            } else if (username.length < 8 || username.length > 20) {
                document.getElementById('usernameError').textContent = '用户名长度必须为8-20个字符';
                document.getElementById('usernameError').style.display = 'block';
                document.getElementById('username').focus();
                return;
            }

            // 验证密码
            if (!password) {
                document.getElementById('passwordError').textContent = '密码不能为空';
                document.getElementById('passwordError').style.display = 'block';
                document.getElementById('password').focus();
                return;
            } else if (password.length < 8 || password.length > 20) {
                document.getElementById('passwordError').textContent = '密码长度必须为8-20个字符';
                document.getElementById('passwordError').style.display = 'block';
                document.getElementById('password').focus();
                return;
            }

            // 验证通过，可以提交表单
            alert('登录信息验证通过！');
            // 这里可以添加实际的表单提交代码
        });
    </script>
</body>
</html>